package com.example.demo.controller;

import com.example.demo.Person;

import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/param")
public class ParamController {
    @RequestMapping("/m1")
    public String m1(String name) {
        return "接收到的参数name:" + name;
    }

    @RequestMapping("/m2")
    public String m2(String name, Integer age) {
        return "接收到的参数name:" + name + ",age:" + age;
    }

    @RequestMapping("/m3")
    public String m3(String name, int age) {
        return "接收到的参数name:" + name + ",age:" + age;
    }

    @RequestMapping("/m4")
    public String m4(Person person) {
        return "接收到的参数person:" + person.toString();
    }

    @RequestMapping("/m5")
    public String m5(@RequestParam(value = "name", required = false) String username) {
        return "接收到的参数name:" + username;
    }

    @RequestMapping("/m6")
    public String m6(String[] arrayParam) {
        return "接收到的参数arrayParam:" + Arrays.toString(arrayParam) + ",长度:" + arrayParam.length;
    }

    @RequestMapping("/m7")
    public String m7(@RequestParam(required = false) List<String> listParam) {
        return "接收到的参数listParam:" + listParam + ",长度:" + listParam.size();
    }

    @RequestMapping("/m8")
    public String m8(@RequestBody Person person) {
        return "接收的数据person:" + person.toString();
    }

    @RequestMapping("/m9/{userId}")
    public String m9(@PathVariable Integer userId) {
        return "userId:" + userId;
    }

    @RequestMapping("/m10")
    public String m10(@RequestPart("file") MultipartFile file) throws IOException {
        String fileName = file.getOriginalFilename();
        file.transferTo(new File("E:/电脑壁纸/" + file.getOriginalFilename()));
        return "success" + fileName;
    }

    @RequestMapping("/getCookie")
    public String getCookie(HttpServletRequest request, HttpServletResponse response) {
        Cookie[] cookies = request.getCookies();//request中拿cookie
//        for (Cookie cookie : cookies) {
//            System.out.println(cookie.getName()+":"+cookie.getValue());
//        }
        //lambda表达式方式写
        if (cookies != null) {//判断空指针异常
            Arrays.stream(cookies).forEach(cookie -> {
                System.out.println(cookie.getName() + ":" + cookie.getValue());
            });
        }

        return "获取cookie成功";
    }

    //开发过程中只获取一个cookie用这种
    @RequestMapping("/getCookie2")
    public String getCookie2(@CookieValue String bite, @CookieValue String aaa) {
        return "cookie存取的值bite:" + bite + ",aaa:" + aaa;
    }

    @RequestMapping("/setSession")
    public String setSession(HttpServletRequest request) {
        HttpSession session = request.getSession();
        session.setAttribute("username", "zhangsan");
        return "success";
    }

    //原始方式
    @RequestMapping("/getSession")
    public String getSession(HttpServletRequest request) {
        HttpSession session = request.getSession(false);
        if (session != null) {
            String username = (String) session.getAttribute("username");
            return "登录用户:" + username;
        }
        return "session 为空";
    }

    @RequestMapping("/getSession2")//下面的@SessionAttribute默认是一个必传参数
    public String getSession2(@SessionAttribute(required = false) String username) {
        return "username:" + username;
    }

    //通过内置对象(需要的时候直接加上，不需要的时候不用去写）
    @RequestMapping("/getSession3")
    public String getSession3(HttpSession session) {//HttpSession session等同于HttpSession session=request.getSession(true)
        String username = (String) session.getAttribute("username");
        return "登录用户:" + username;
    }

    @RequestMapping("/getHeader")
    public String getHeader(HttpServletRequest request) {
        String userAgent = request.getHeader("User-Agent");//User—Agent获取使用是哪个浏览器
        return "userAgent:" + userAgent;
    }

    @RequestMapping("/getHeader2")
    public String getHeader2(@RequestHeader("User-Agent") String userAgent) {//通过注解的方式拿取信息
        return "userAgent:" + userAgent;
    }
}
